﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;
using Account = KeRui.BLL.Account;
using System.Text;
using System.Collections.Generic;

public partial class Manage_Cms_NodeItem : System.Web.UI.Page
{
    public string Act = ManageHelper.PageAct();
    protected ManagePageHelper pageHelper = new ManagePageHelper();
    private KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
    protected KeRui.Model.CmsNode modelCmsNode = new CmsNode();
    protected KeRui.BLL.CmsTemplateFile bllCmsTemplateFile = new KeRui.BLL.CmsTemplateFile();
    protected int siteid = PageRequest.GetInt("siteid", -1);
    protected string ActName = "添加";
    protected string ActFlag = "addsave";
    protected DataSet dsCmsModel = new DataSet();
    protected DataSet dsNoteTemp = new DataSet();
    protected DataSet dsContentTemp = new DataSet();
    protected DataSet dsCommentTemp = new DataSet();
    ManageErrMsg nsb = new ManageErrMsg();

    protected KeRui.Model.CmsSite modelCmsSite = new CmsSite();
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//登陆判断
        KeRui.BLL.CmsModel bllCmsModle = new KeRui.BLL.CmsModel();
        dsCmsModel = bllCmsModle.GetSelect();
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        modelCmsSite = bllCmsSite.GetModel(siteid);
        KeRui.BLL.CmsTemplate bllCmsTemplate = new KeRui.BLL.CmsTemplate();
        KeRui.Model.CmsTemplate modelCmsTemplate = bllCmsTemplate.GetModel(modelCmsSite.ServerID, modelCmsSite.TemplateFolder);
        dsNoteTemp = bllCmsTemplateFile.GetSelect(modelCmsSite.TemplateID, 2);//栏目模板
        dsContentTemp = bllCmsTemplateFile.GetSelect(modelCmsSite.TemplateID, 3);//内容模板
        dsCommentTemp = bllCmsTemplateFile.GetSelect1(modelCmsSite.TemplateID, 5);//评论模板
        modelCmsNode.ParentID = PageRequest.GetInt("PID", 0);
        switch (Act)
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            case "sort":
                sort();
                break;
            default: break;

        }
    }

    #region 添加保存
    private void Add()
    {
        ManageHelper.CheckAdminPower("site_nodeadd_"+siteid);//权限

        modelCmsNode.ParentID = PageRequest.GetInt("ParentID", 0);
        //if (modelCmsNode.ParentID == -1)
        //{
        //    nsb.AddErr("Error:父栏目ID没得到。");
        //    nsb.ChkErr();
        //}
        modelCmsNode.NodeName = PageRequest.GetString("NodeName");
        if (modelCmsNode.NodeName == "")
        {
            nsb.AddErr("请输入栏目名称。");
        }
        modelCmsNode.NodeKey = PageRequest.GetString("NodeKey");
        if (modelCmsNode.NodeKey == "")
        {
            nsb.AddErr("请输入栏目索引名称。");
        }
        modelCmsNode.NodeType = PageRequest.GetInt("NodeType", -1);
        if (modelCmsNode.NodeType == -1)
        {
            nsb.AddErr("Error:节点内容模型没得到。"); nsb.ChkErr();
        }
        modelCmsNode.ContentDefaultTemplateFile = PageRequest.GetString("ContentDefaultTemplateFile");
        modelCmsNode.TemplateFile = PageRequest.GetString("TemplateFile");
        modelCmsNode.ModelID = PageRequest.GetInt("ModelID", -1);
        modelCmsNode.NodeLinkUrl = PageRequest.GetString("NodeLinkUrl");
        if (modelCmsNode.NodeType == 1 || modelCmsNode.NodeType == 4)//列表节点　1
        {
            if (modelCmsNode.ModelID == -1)
            {
                nsb.AddErr("Error:没有得到内容模型。"); nsb.ChkErr();
            }
            if (modelCmsNode.TemplateFile == "")
            {
                nsb.AddErr("Error:栏目页模板文件没得到。"); nsb.ChkErr();
            }
            if (modelCmsNode.ContentDefaultTemplateFile == "")
            {
                nsb.AddErr("Error:内容页模板文件没得到。"); nsb.ChkErr();
            }
        }
        else if (modelCmsNode.NodeType == 2)//单页节点　2
        {
           
            if (modelCmsNode.TemplateFile == "")
            {
                nsb.AddErr("Error:栏目页模板文件没得到。"); nsb.ChkErr();
            }
           
        }
        else//外部链接  3
        {
            if (modelCmsNode.NodeLinkUrl == "")
            {
                nsb.AddErr("外部链接地址不能为空。");
            }
        }
        modelCmsNode.CommentTemplate = PageRequest.GetString("CommentTemplate");
     
        modelCmsNode.ContentPageSize = PageRequest.GetInt("ContentPageSize", -1);
        if (modelCmsNode.ContentPageSize<=0)
        {
            nsb.AddErr("列表页分页条数只能为一个整数");
        }
        modelCmsNode.NodePicUrl = PageRequest.GetString("NodePicUrl");//须处理
        modelCmsNode.MetaKeywords = PageRequest.GetString("MetaKeywords");
        modelCmsNode.MetaDescription = PageRequest.GetString("MetaDescription");
        modelCmsNode.ShowOnMenu = PageRequest.GetInt("ShowOnMenu", 0) == 1 ? true : false;


        KeRui.Model.CmsNode PmodelCmsNode = new CmsNode();
        PmodelCmsNode = bllCmsNode.GetModel(modelCmsNode.ParentID);
        if (PmodelCmsNode.ParentID != 0)
        {
            modelCmsNode.NodePath = PmodelCmsNode.NodePath + @"\" + modelCmsNode.NodeKey;
            modelCmsNode.NodeDir = PmodelCmsNode.NodeDir + @"\" + modelCmsNode.NodeKey;
        }
        else
        {
            modelCmsNode.NodePath = modelCmsNode.NodeKey;
            modelCmsNode.NodeDir = modelCmsSite.SiteDir + @"\" + modelCmsNode.NodeKey;
        }
        modelCmsNode.NodePath = modelCmsNode.NodePath.Replace(@"\", "/");
        modelCmsNode.SiteID = siteid;
        modelCmsNode.Description = "";
        modelCmsNode.Tips = "";
        KeRui.BLL.Account bllAccount = new Account();
        modelCmsNode.CreateUser = bllAccount.GetModel(KeRui.BLL.Account.GetLoginUserID()).Username;
        int NodeID =-1;
        nsb.ChkErr();
        try
        {
            if (PmodelCmsNode.ParentID != 0)
            {
                FileObj.FolderCreate(PmodelCmsNode.NodeDir, modelCmsNode.NodeKey);
            }
            else
            {
                FileObj.FolderCreate(modelCmsSite.SiteDir, modelCmsNode.NodeKey);
            }
            NodeID = bllCmsNode.Add(modelCmsNode);
        }
        catch(Exception ex) {
            nsb.AddErr(ex.ToString()); nsb.ChkErr();
        }

        modelCmsNode = bllCmsNode.GetModel(NodeID);
        ManageHelper.AddLog("site_nodeadd", "添加了“" + modelCmsNode.NodeName + "”栏目。");
        Response.Write("<script type=\"text/javascript\">window.parent.location.href='Nodelist.aspx?pnode=" + modelCmsNode.ParentPath + "&siteId=" + siteid + "&sucmsg=添加成功。'</script>");
    }
    #endregion

    #region 反显修改信息
    private void Edit()
    {
        ActName = "修改";
        ActFlag = "EditSave";
        int NodeID = PageRequest.GetInt("NodeID", -1);
        if (NodeID == -1)
        {
            nsb.AddErr("Error：没有获取节点NodeID。"); nsb.ChkErr();
        }
        modelCmsNode = bllCmsNode.GetModel(NodeID);
        if (modelCmsNode == null)
        {
            nsb.AddErr("Error：没有得到ModelCmsNode。"); nsb.ChkErr();
        }
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("site_nodeedit_" + siteid);//权限
        modelCmsNode.NodeID = PageRequest.GetInt("NodeID", -1);
        if (modelCmsNode.NodeID == -1)
        {
            nsb.AddErr("Error:没有得到NodeID"); nsb.ChkErr();
        }
        modelCmsNode.ParentID = PageRequest.GetInt("ParentID", 0);
        //if (modelCmsNode.ParentID == -1)
        //{
        //    nsb.AddErr("Error:父栏目ID没得到。");
        //    nsb.ChkErr();
        //}
        modelCmsNode.NodeName = PageRequest.GetString("NodeName");
        if (modelCmsNode.NodeName == "")
        {
            nsb.AddErr("请输入栏目名称。");
        }
        modelCmsNode.NodeType = PageRequest.GetInt("NodeType", -1);
        if (modelCmsNode.NodeType == -1)
        {
            nsb.AddErr("Error:节点内容模型没得到。"); nsb.ChkErr();
        }
        modelCmsNode.ContentDefaultTemplateFile = PageRequest.GetString("ContentDefaultTemplateFile");
        modelCmsNode.TemplateFile = PageRequest.GetString("TemplateFile");
        modelCmsNode.ModelID = PageRequest.GetInt("ModelID", -1);
        modelCmsNode.NodeLinkUrl = PageRequest.GetString("NodeLinkUrl");
        if (modelCmsNode.NodeType == 1 || modelCmsNode.NodeType == 4)//列表节点　1
        {
            if (modelCmsNode.ModelID == -1)
            {
                nsb.AddErr("Error:没有得到内容模型。"); nsb.ChkErr();
            }
            if (modelCmsNode.TemplateFile == "")
            {
                nsb.AddErr("Error:栏目页模板文件没得到。"); nsb.ChkErr();
            }
            if (modelCmsNode.ContentDefaultTemplateFile == "")
            {
                nsb.AddErr("Error:内容页模板文件没得到。"); nsb.ChkErr();
            }
        }
        else if (modelCmsNode.NodeType == 2)//单页节点　2
        {
            //if (modelCmsNode.ModelID == -1)
            //{
            //    nsb.AddErr("Error:没有得到内容模型。"); nsb.ChkErr();
            //}
            if (modelCmsNode.TemplateFile == "")
            {
                nsb.AddErr("Error:栏目页模板文件没得到。"); nsb.ChkErr();
            }
            //if (modelCmsNode.ContentDefaultTemplateFile == "")
            //{
            //    nsb.AddErr("Error:内容页模板文件没得到。"); nsb.ChkErr();
            //}
        }
        else//外部链接  3
        {
            if (modelCmsNode.NodeLinkUrl == "")
            {
                nsb.AddErr("外部链接地址不能为空。");
            }
        }
        modelCmsNode.CommentTemplate = PageRequest.GetString("CommentTemplate");
        //if (modelCmsNode.CommentTemplate == "")
        //{
        //    nsb.AddErr("Error:评论模板文件没得到。"); nsb.ChkErr();
        //}
        modelCmsNode.ContentPageSize = PageRequest.GetInt("ContentPageSize", -1);
        if (modelCmsNode.ContentPageSize <= 0)
        {
            nsb.AddErr("列表页分页条数只能为一个整数");
        }
        modelCmsNode.NodePicUrl = PageRequest.GetString("NodePicUrl");//须处理
        modelCmsNode.MetaKeywords = PageRequest.GetString("MetaKeywords");
        modelCmsNode.MetaDescription = PageRequest.GetString("MetaDescription");
        modelCmsNode.ShowOnMenu = PageRequest.GetInt("ShowOnMenu", 0) == 1 ? true : false;
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        KeRui.Model.CmsSite modelCmsSite = bllCmsSite.GetModel(siteid);
        modelCmsNode.Description = "";
        modelCmsNode.Tips = "";
        KeRui.BLL.Account bllAccount = new Account();
        modelCmsNode.CreateUser = bllAccount.GetModel(KeRui.BLL.Account.GetLoginUserID()).Username;
        nsb.ChkErr();
        bllCmsNode.Update(modelCmsNode); 
        ManageHelper.AddLog("site_nodeedit", "修改了“" + modelCmsNode.NodeName + "”栏目。");
        modelCmsNode = bllCmsNode.GetModel(modelCmsNode.NodeID);
        Response.Write("<script type=\"text/javascript\">window.parent.location.href='Nodelist.aspx?pnode=" + modelCmsNode.ParentPath + "&siteId=" + siteid + "&sucmsg=修改成功。'</script>");
    }
    #endregion 

    #region 栏目下拉列表
    public string SelectNodeList(int pId, int intTemp, int nodeID)
    {
        string RetStr = "";
        List<KeRui.Model.CmsNode> list = bllCmsNode.GetListByPId(pId, siteid, nodeID);
        int j = 0;
        foreach (KeRui.Model.CmsNode model in list)
        {
            j++;
            string Empty = "";
            for (int i = 0; i < model.Depth; i++)
            {
                Empty += "　";
            }
            if (model.NodeID == intTemp)
            {
                RetStr += "<option value=" + model.NodeID + " selected>" + Empty + "{0}" + model.NodeName + "</option>";
            }
            else
            {
                RetStr += "<option value=" + model.NodeID + ">" + Empty + "{0}" + model.NodeName + "</option>";
            }
            if (model.ChildNum > 0)
            {
                RetStr += SelectNodeList(model.NodeID, intTemp, nodeID);
            }
            if (j == list.Count)
            {
                RetStr = RetStr.Replace("{0}", "└");
            }
            else
            {
                RetStr = RetStr.Replace("{0}", "├");
            }
        }
        return RetStr.ToString();
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("site_nodedel_" + siteid);//权限
        int NodeID = PageRequest.GetInt("ID", -1);
        if (siteid == -1)
        {
            nsb.AddErr("Error：没有得到SiteID。"); nsb.ChkErr();
        }
        if (NodeID == -1)
        {
            nsb.AddErr("error：没有得到NodeID。"); nsb.ChkErr();
        }
        modelCmsNode = bllCmsNode.GetModel(NodeID);
        if (modelCmsNode == null)
        {
            nsb.AddErr("你所要删除的信息不存在"); nsb.ChkErr();
        }
        if (bllCmsNode.Delete(NodeID) > 0)
        {
            ManageHelper.AddLog("site_nodedel_" + siteid, "删除了“" + modelCmsNode.NodeName + "”栏目,ID为" + modelCmsNode.NodeID + "。");
            Response.Redirect("Nodelist.aspx?SiteID=" + siteid + "&sucmsg=恭喜！删除成功。");
        }
        else
        {
            Response.Redirect("Nodelist.aspx?SiteID=" + siteid + "&errmsg=对不起删除失败！原因：该栏目下存在子栏目。");
        }
    }
    #endregion

    #region 以前代码
    private void add_xs()
    {
        //    string[] split = new string[1];
        //split[0] = "\r";
        //string[] nodes = PageRequest.GetString("nodenames").Split(split, StringSplitOptions.None);
        //int rootID = PageRequest.GetInt("parentnodeid", -1);
        //KeRui.BLL.Account bllAccount = new Account();
        //string userName = bllAccount.GetModel(KeRui.BLL.Account.GetLoginUserID()).Username;
        //KeRui.Model.CmsNode modelRootNode = bllCmsNode.GetModel(rootID);
        //for (int i = 0; i < nodes.Length; i++)
        //{
        //    KeRui.Model.CmsNode modelCmsNode = new CmsNode();
        //    modelCmsNode.RootID = rootID;
        //    modelCmsNode.arrChildID = string.Empty;
        //    modelCmsNode.ChildNum = 0;
        //    modelCmsNode.CreateUser = userName;
        //    modelCmsNode.NodeName = nodes[i];
        //    modelCmsNode.NodeKey = PageRequest.GetString("nodeindex");
        //    modelCmsNode.NodePath = modelRootNode.NodePath + "\\" + PageRequest.GetString("nodeindex");
        //    modelCmsNode.NodeDir = modelRootNode.NodeDir + "\\" + PageRequest.GetString("nodeindex");
        //    modelCmsNode.SiteID = siteid;
        //    modelCmsNode.ShowOnMenu = true;
        //    modelCmsNode.ModelID = 0;
        //    modelCmsNode.ParentID = rootID;
        //    modelCmsNode.NodeType = 1; //1-为列表节点 2-为单页节点 3-为外部链接
        //    modelCmsNode.PlusField = string.Empty;
        //    modelCmsNode.NodePicUrl = string.Empty;
        //    modelCmsNode.NodeLinkUrl = string.Empty;
        //    modelCmsNode.MetaKeywords = string.Empty;
        //    modelCmsNode.MetaDescription = string.Empty;
        //    modelCmsNode.ParentPath = modelRootNode.ParentPath + "," + modelRootNode.NodeID;
        //    modelCmsNode.Tips = string.Empty;
        //    modelCmsNode.Description = string.Empty;
        //    modelCmsNode.Depth = modelRootNode.Depth + 1;
        //    int currentNodeid = bllCmsNode.Add(modelCmsNode);
        //    foreach (string nodeid in modelCmsNode.ParentPath.Split(','))
        //    {
        //        if (nodeid != "0")
        //        {
        //            bllCmsNode.UpdateNodeInfo(Convert.ToInt16(nodeid), currentNodeid);
        //        }
        //    }
        //    Response.Write(
        //        string.Format(
        //            "<script type="text/javascript">window.parent.location.href=\"NodeList.aspx?siteid={0}\"</script>",
        //            PageRequest.GetInt("siteid", -1)));
        //    Response.End();
        //}
        //case "checkexist":
        //string nodeIndex = PageRequest.GetString("nodeindex");
        //int rootid = PageRequest.GetInt("rootid", -1);
        //break;
    }
    #endregion

    #region 排序
    private void sort()
    {
        int NodeID = PageRequest.GetInt("ID", -1);
        int flag = PageRequest.GetInt("f", 0);
        if (NodeID == -1)
        {
            nsb.AddErr("error：没有找到NodeID"); nsb.ChkErr();
        }
        if (flag == 0)
        {
            nsb.AddErr("Error：没有找到Flag。"); nsb.ChkErr();
        }

        int intret = bllCmsNode.sort(NodeID, flag);
        modelCmsNode = bllCmsNode.GetModel(NodeID);
        Response.Redirect("Nodelist.aspx?play=" + modelCmsNode.ParentID + "&SiteID=" + siteid);
    }
    #endregion

}
